//
//  GameDataManager.m
//  CardWarrior
//
//  Created by Tekka Xie on 12-4-22.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "GameDataManager.h"

@implementation GameDataManager

static GameDataManager* pInstance = nil;

+ (id)sharedInstance
{
    if (pInstance == nil)
        pInstance = [[GameDataManager alloc] init];    
    return pInstance;
}

// 游戏永久数据
@synthesize bgm;
@synthesize sfx;
@synthesize difficulty;
@synthesize roles;
@synthesize hostWinCount;
@synthesize guestWinCount;

// 游戏intermediate数据
@synthesize gameCurrentMode;
@synthesize hostSelectedRole;
@synthesize guestSelectedRole;
@synthesize whoIsSelecting;
@synthesize hostScore;
@synthesize guestScore;
@synthesize whoWin;

- (id)init
{
    if ((self = [super init]) == nil)
        return nil; 
        
    NSString *pInfoListPath = [[NSBundle mainBundle] pathForResource:@"gamedata" ofType:@"plist"];
    NSData *pInfoListXML = [[NSFileManager defaultManager] contentsAtPath:pInfoListPath];
    
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSDictionary *dataDictionary = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:pInfoListXML
                                              mutabilityOption:0
                                                        format:&format
                                              errorDescription:&errorDesc];
    if (!dataDictionary) 
    {
        NSAssert2(0, @"Error reading plist: %@, format: %d", errorDesc, format);
        return nil;
    }
    
    // 1. 登记数据
    self.bgm = [(NSNumber*)[dataDictionary objectForKey:@"bgm"] intValue];
    self.sfx = [(NSNumber*)[dataDictionary objectForKey:@"sfx"] intValue];
    self.difficulty = [(NSNumber*)[dataDictionary objectForKey:@"difficulty"] intValue];
    
    self.roles = [NSMutableArray arrayWithArray:[dataDictionary objectForKey:@"roles"]];
    
    self.hostWinCount = [(NSNumber*)[dataDictionary objectForKey:@"hostWinCount"] intValue];
    self.guestWinCount = [(NSNumber*)[dataDictionary objectForKey:@"guestWinCount"] intValue];
    
    self.gameCurrentMode = [(NSNumber*)[dataDictionary objectForKey:@"gameCurrentMode"] intValue];
    self.hostSelectedRole = [(NSNumber*)[dataDictionary objectForKey:@"hostSelectedRole"] intValue];
    self.guestSelectedRole = [(NSNumber*)[dataDictionary objectForKey:@"guestSelectedRole"] intValue];
    self.whoIsSelecting = [(NSNumber*)[dataDictionary objectForKey:@"whoIsSelecting"] intValue];
    self.hostScore = [(NSNumber*)[dataDictionary objectForKey:@"hostScore"] intValue];
    self.guestScore = [(NSNumber*)[dataDictionary objectForKey:@"guestScore"] intValue];
    self.whoWin = [(NSNumber*)[dataDictionary objectForKey:@"whoWin"] intValue];
    
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString* psaveDataPath = [rootPath stringByAppendingPathComponent:@"userdata.plist"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:psaveDataPath]) 
    {
        [self saveData];
    }
    else 
    {
        NSString *errorDesc2 = nil;
        NSPropertyListFormat format2;
            
        NSData *plistXML2 = [[NSFileManager defaultManager] contentsAtPath:psaveDataPath];
        NSDictionary* saveData = (NSDictionary *)[NSPropertyListSerialization
                                                    propertyListFromData:plistXML2
                                                        mutabilityOption:0
                                                          format:&format
                                                          errorDescription:&errorDesc];
        if (!saveData) 
        {
            NSLog(@"Error reading plist: %@, format: %d", errorDesc2, format2);
            return nil;
        }

        // 2. 重新读取用户数据
        self.difficulty = [(NSNumber*)[saveData objectForKey:@"difficulty"] intValue];

        self.bgm = [(NSNumber*)[saveData objectForKey:@"bgm"] intValue];
        self.sfx = [(NSNumber*)[saveData objectForKey:@"sfx"] intValue];
        
        self.hostWinCount = [(NSNumber*)[saveData objectForKey:@"hostWinCount"] intValue];
        self.guestWinCount = [(NSNumber*)[saveData objectForKey:@"guestWinCount"] intValue];
        
        self.gameCurrentMode = [(NSNumber*)[saveData objectForKey:@"gameCurrentMode"] intValue];
        self.hostSelectedRole = [(NSNumber*)[saveData objectForKey:@"hostSelectedRole"] intValue];
        self.guestSelectedRole = [(NSNumber*)[saveData objectForKey:@"guestSelectedRole"] intValue];
        self.whoIsSelecting = [(NSNumber*)[saveData objectForKey:@"whoIsSelecting"] intValue];
        self.hostScore = [(NSNumber*)[saveData objectForKey:@"hostScore"] intValue];
        self.guestScore = [(NSNumber*)[saveData objectForKey:@"guestScore"] intValue];
        self.whoWin = [(NSNumber*)[saveData objectForKey:@"whoWin"] intValue];

    }
    return self;
}
    
-(void) saveData 
{
    @autoreleasepool {
    NSString *errorDesc = nil;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"userdata.plist"];
        
    // 3.保存用户数据
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                            [NSArray arrayWithObjects: [NSNumber numberWithInt:self.bgm],
                                                       [NSNumber numberWithInt:self.sfx],
                                                       [NSNumber numberWithInt:self.difficulty],
                                                       [NSNumber numberWithInt:self.hostWinCount],
                                                       [NSNumber numberWithInt:self.guestWinCount],
                                                       [NSNumber numberWithInt:self.gameCurrentMode],
                                                       [NSNumber numberWithInt:self.hostSelectedRole],
                                                       [NSNumber numberWithInt:self.guestSelectedRole],
                                                       [NSNumber numberWithInt:self.whoIsSelecting],
                                                       [NSNumber numberWithInt:self.hostScore],
                                                       [NSNumber numberWithInt:self.guestScore],
                                                       [NSNumber numberWithInt:self.whoWin],
                                                        nil]
                               // 4. 添加记录
                            forKeys:[NSArray arrayWithObjects:@"bgm", @"sfx", @"difficulty", 
                                                              @"hostWinCount", @"guestWinCount", 
                                                              @"gameCurrentMode", @"hostSelectedRole", 
                                                              @"guestSelectedRole", @"whoIsSelecting",
                                                              @"hostScore", @"guestScore", @"whoWin",
                                                              nil]];        
        
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&errorDesc];
        
    if(plistData) 
    {
        [plistData writeToFile:plistPath atomically:YES];
    }
    }
}

- (void)switchDiffculty
{
    difficulty++;
    if (difficulty > 3)
        difficulty -= 3;
}

- (void)switchSFX
{
    sfx = !sfx;
}

- (void)switchBGM
{
    bgm = !bgm;
}

- (void)minusHostSelectedRole
{
    hostSelectedRole = (--hostSelectedRole+8)%8;
}

- (void)addHostSelectedRole
{
    hostSelectedRole = (++hostSelectedRole+8)%8;
}

- (void)minusGuestSelectedRole
{
    guestSelectedRole = (--guestSelectedRole+8)%8;
}

- (void)addGuestSelectedRole
{
    guestSelectedRole = (++guestSelectedRole+8)%8;
}

- (void)resetScore
{
    hostScore = 25;
    guestScore = 25;
}

- (void)minusHostScore
{
    hostScore--;
}

- (void)minusGuestScore
{
    guestScore--;
}

- (void)clearPkData
{
    whoIsSelecting = 1;
    hostScore = 25;
    guestScore = 25;
}

@end

